function JRDLogGrowthModelOptim = JRDLogGrowthModelFullRefFixL_Study2(x)
global newTestProbe
global obsRespErr
global refDirectionFull
global layout

pointResponse = [];

% Create a matrix for reference directions and specify num of objects and
% IDs
if refDirectionFull == 1 && layout == 1
    refDirection = [0,1; 1,0; 0,-1; -1,0];
elseif refDirectionFull == 1 && layout ~= 1
    refDirection = [0,1; 1,0; 0,-1; -1,0; 1,-1];
else
    refDirection = [1,-1];
end
headingConditions = [0,45,90,135,180,225,270,315];

% Create a matrix containing the locations of all objects relative to an
% origin (obj id is the index)
if layout == 1
    locRelOrigin = [-1,1; 0,1; 1,1; -1,0; 0,0; 1,0; 0,-1];
else
    locRelOrigin = [0,1; -1,0; 0,0; 1,0; -1,-1; 0,-1; 1,-1];
end

% Create cell array; each index is an item containing a n by 2 matrix of 
% vectors representing the distances of each item from that location
% relative to the reference axes. The encoded object locations are then
% computed from those.
for i=1:length(locRelOrigin(:,1))
    for j=1:length(locRelOrigin(:,1))
        locRelObj{i}(j,1) = locRelOrigin(j,1) - locRelOrigin(i,1);
        locRelObj{i}(j,2) = locRelOrigin(j,2) - locRelOrigin(i,2);
    end
end

% Organize test probes by heading and allocentric pointing direction (round
% to get rid of strange decimals)
for i=1:length(newTestProbe(:,1))
    if thetaFunc([1,0],locRelObj{newTestProbe(i,1)}(newTestProbe(i,2),:)) <= ... 
            thetaFunc([-1,0],locRelObj{newTestProbe(i,1)}(newTestProbe(i,2),:))
        newTestProbe(i,4) = round(thetaFunc([0,1],locRelObj{newTestProbe(i,1)}(newTestProbe(i,2),:)));
    else
        newTestProbe(i,4) = round(180 + thetaFunc([0,-1],locRelObj{newTestProbe(i,1)}(newTestProbe(i,2),:)));
    end
    if thetaFunc([1,0],locRelObj{newTestProbe(i,1)}(newTestProbe(i,3),:)) <= ... 
            thetaFunc([-1,0],locRelObj{newTestProbe(i,1)}(newTestProbe(i,3),:))
        newTestProbe(i,5) = round(thetaFunc([0,1],locRelObj{newTestProbe(i,1)}(newTestProbe(i,3),:)));
    else
        newTestProbe(i,5) = round(180 + thetaFunc([0,-1],locRelObj{newTestProbe(i,1)}(newTestProbe(i,3),:)));
    end
end

% Objects with noisy encoding
    for i=1:length(locRelOrigin(:,1))
        for j=1:length(locRelOrigin(:,1))
            for k=1:length(refDirection(:,1))
                angleHolder(k) = thetaFunc(refDirection(k,:),locRelObj{i}(j,:));           
            end
            [M,I] = min(angleHolder);
            locEncoded{i}(j,1) = locRelObj{i}(j,1) + normrnd(0,(x(1)/(1+x(2)*exp(-(1)*thetaFunc(refDirection(I,:),locRelObj{i}(j,:))))));
            locEncoded{i}(j,2) = locRelObj{i}(j,2) + normrnd(0,(x(1)/(1+x(2)*exp(-(1)*thetaFunc(refDirection(I,:),locRelObj{i}(j,:))))));
            if i > 1
                while i - j > 0    
                    locEncoded{i}(i-j,:) = -(locEncoded{i-j}(i,:));
                    j = j + 1;
                end
            end
        end
    end
    
    % Now an algorithm to cycle through the test probes and perform the JRD
    % task
    for i=1:length(newTestProbe(:,1))

        % Define standing position, imagined heading, and pointing direction
        standpos = newTestProbe(i,1);
        imagheading = newTestProbe(i,2);
        pointobj = newTestProbe(i,3);

        % Create arrays for heading condition and correct pointing direction
        if thetaFunc([1,0],locRelObj{standpos}(imagheading,:)) <= ... 
                thetaFunc([-1,0],locRelObj{standpos}(imagheading,:))        
            headingCond(i) = thetaFunc([0,1],locRelObj{standpos}(imagheading,:));
        else
            headingCond(i) = 180 + thetaFunc([0,-1],locRelObj{standpos}(imagheading,:));
        end
        % Get rid of strange decimals
        headingCond(i) = round(headingCond(i));
        
        correctPointAngle(i) = atanThetaFunc(locRelObj{standpos}(imagheading,:), ...
            locRelObj{standpos}(pointobj,:));
        % Get rid of strange decimals
        correctPointAngle(i) = round(correctPointAngle(i));

        % Create array for pointing condition
        if thetaFunc([1,0],locRelObj{standpos}(pointobj,:)) <= ... 
                thetaFunc([-1,0],locRelObj{standpos}(pointobj,:))        
            pointingCond(i) = thetaFunc([0,1],locRelObj{standpos}(pointobj,:));
        else
            pointingCond(i) = 180 + thetaFunc([0,-1],locRelObj{standpos}(pointobj,:));
        end
        % Get rid of strange decimals
        pointingCond(i) = round(pointingCond(i));

        % Check if imagined heading is consistent with reference direction
        for j=1:length(refDirection(:,1))
            if round(thetaFunc(locEncoded{standpos}(imagheading,:),refDirection(j,:))) <= 20
                pointResponse(i) = atanThetaFunc(refDirection(j,:), ... 
                    locEncoded{standpos}(pointobj,:));
                break
            end
        end

        % If imagined direction is not in line, need to do additional
        % computaions (vecter addition)
        if length(pointResponse) ~= i
            angleVector = locEncoded{standpos}(imagheading,:) + ... 
                locEncoded{imagheading}(pointobj,:);
            pointResponse(i) = atanThetaFunc(locEncoded{standpos}(imagheading,:), ... 
                angleVector);
        end  
        
    end

    for i=1:length(pointResponse)
        pointingError(i) = abs(pointResponse(i) - correctPointAngle(i));
        if pointingError(i) > 180
            pointingError(i) = 360 - pointingError(i);
        end
        SE(i) = (pointingError(i) - obsRespErr(i)).^2;      
    end
    
    SSE = sum(SE) + 5*(x(1).^2 + x(2).^2);
    JRDLogGrowthModelOptim = SSE;
    
end